package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.R.R;
import net.wanho.dto.RoleAuthDTO;
import net.wanho.dto.RoleDTO;
import net.wanho.dto.RoleExportDTO;
import net.wanho.po.Role;
import net.wanho.service.RoleAuthService;
import net.wanho.service.RoleService;
import net.wanho.vo.PageInfo;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 这个类是：
 *
 * @author: MENG HU WANG
 * @date: 2025/1/28 17:08
 * @version: 1.0
 */

@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;
    private final RoleAuthService roleAuthService;

    // 所有角色列表
    @GetMapping("role-list")
    public R roleList() {
        List<Role> list = roleService.list();
        return R.ok(list);
    }

    // 分页查询角色列表
    @GetMapping("/role-page-list")
    public R getRolePageList(@RequestParam(required = false, defaultValue = "1") int pageNum,
                             @RequestParam(required = false, defaultValue = "5") int pageSize,
                             RoleDTO roleDTO) {
        PageInfo<Role> pageInfo = roleService.getRolePageList(pageNum, pageSize, roleDTO);
        return R.ok(pageInfo);
    }

    // 添加角色
    @PostMapping("/role-add")
    public R addRole(@RequestBody Role role) {
        roleService.addRole(role);
        return R.ok("添加成功");
    }

    // 修改角色状态
    @PutMapping("role-state-update")
    public R updateRoleState(@RequestBody Role role) {
        roleService.updateById(role);
        return R.ok("修改成功");
    }

    // 根据ID查询角色信息
    @GetMapping("/{id}")
    public R getRoleById(@PathVariable("id") Integer id) {
        Role role = roleService.selectRoleById(id);

        return R.ok(role);
    }

    // 编辑角色信息
    @PutMapping("/role-update")
    public R updateRole(@RequestBody Role role) {
        roleService.updateById(role);
        return R.ok("更新成功");
    }

    // 删除角色
    @DeleteMapping("/role-delete/{id}")
    public R deleteRoleById(@PathVariable("id") Integer id) {
        roleService.removeById(id);
        return R.ok("删除成功");
    }

    // 根据ID查询权限
    @GetMapping("/role-auth")
    public R getRoleAuth(Integer roleId) {
        List<Integer> list = roleAuthService.getByUserId(roleId);
        return R.ok(list);
    }

    // 更改角色的权限
    @PutMapping("/auth-grant")
    public R grantAuth(@RequestBody RoleAuthDTO roleDTO) {
        roleAuthService.updateAuth(roleDTO);
        return R.ok("授权成功");
    }

    // 导出数据
    @GetMapping("/exportTable")
    public R exportTable(RoleExportDTO roleExportDTO) {
//        List<Role> list = roleService.exportTable(pageNum, pageSize, role);
        List<Role> list = roleService.exportTable(roleExportDTO);
        return R.ok("导出成功", list);
    }
}
